001    /*
002     * Copyright 2006 Stephen J. McConnell.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.metro.data;
020    
021    import java.io.IOException;
022    import java.io.Writer;
023    
024    import net.dpml.component.Controller;
025    import net.dpml.component.Composition;
026    
027    import net.dpml.lang.Classpath;
028    import net.dpml.lang.Info;
029    
030    import net.dpml.metro.builder.ComponentEncoder;
031    
032    import net.dpml.util.Logger;
033    
034    
035    /**
036     * Component composition.
037     *
038     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
039     * @version 1.0.1
040     */
041    public final class DefaultComposition extends Composition
042    {   
043        private ComponentDirective m_directive;
044        
045        private final Classpath m_classpath;
046        
047       /**
048        * Creation of a new composition diefinition.
049        * @param logger the assigned logging channel
050        * @param info the part info definition
051        * @param classpath the part classpath definition
052        * @param controller the deployment controller
053        * @param directive the deployment directive
054        * @exception IOException if an I/O exception occurs
055        */
056        public DefaultComposition( 
057          Logger logger, Info info, Classpath classpath, Controller controller, ComponentDirective directive )
058          throws IOException
059        {
060            super( logger, info, classpath, controller, directive, directive.getName() );
061            
062            m_directive = directive;
063            
064            if( null != m_directive.getBaseDirective() )
065            {
066                // override the classpath defintion with a extended definition
067                
068                DefaultComposition composition = m_directive.getBasePart();
069                Classpath base = composition.getClasspath();
070                m_classpath = new Classpath( base, classpath );
071            }
072            else
073            {
074                m_classpath = null;
075            }
076        }
077        
078       /**
079        * Get the deployment directive.
080        * @return the deployment directive
081        */
082        public ComponentDirective getComponentDirective()
083        {
084            return m_directive;
085        }
086        
087       /**
088        * Get the part classpath definition.
089        *
090        * @return the classpath definition
091        */
092        public Classpath getClasspath()
093        {
094            if( null != m_classpath )
095            {
096                return m_classpath;
097            }
098            else
099            {
100                return super.getClasspath();
101            }
102        }
103    
104       /**
105        * Encode the deployment directive to XML.
106        * @param writer the output stream writer
107        * @param pad the outoput offset
108        * @exception IOException if an I/O exception occurs
109        */
110        protected void encodeStrategy( Writer writer, String pad ) throws IOException
111        {
112            ComponentEncoder encoder = new ComponentEncoder();
113            encoder.writeComponent( writer, m_directive, pad );
114        }
115        
116       /**
117        * Return true if this object is equal to the supplied object.
118        * @param other the object to evaluate
119        * @return the equality status
120        */
121        public boolean equals( Object other )
122        {
123            if( super.equals( other ) && ( other instanceof DefaultComposition ) )
124            {
125                DefaultComposition composite = (DefaultComposition) other;
126                return m_directive.equals( composite.m_directive );
127            }
128            else
129            {
130                return false;
131            }
132        }
133    
134       /**
135        * Return the hashcode for the instance.
136        * @return the instance hashcode
137        */
138        public int hashCode()
139        {
140            int hash = super.hashCode();
141            hash ^= m_directive.hashCode();
142            return hash;
143        }
144    }